﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Drawer
{

    public partial class MainForm : Form
    {
        int start = 0,count = 0;
        byte temp = 0, updata_USART = 0,aa = 0, change_unit = 0;
        int  ps_p, ps_t, qc_p, qc_t;
        byte No = 164;
        byte[] P0 = new byte[3] { 1, 139, 193 };
        byte[] cod0 = new byte[4] { 112, 6, 170 , 170 };
        byte[] cod1 = new byte[4] { 112, 25, 170 , 170 };
        byte[] cod2 = new byte[4] { 112, 16, 170 , 170 };
        byte[] cod3 = new byte[4] { 112, 32, 170, 170 };
        byte[] cod4 = new byte[4] { 112, 1, 170, 170 };
        byte[] usartget_data = new byte[35];
        byte P_unit = 165;//mmHg
        byte T_unit = 161;//℃
        byte us_i = 0, us_t = 0, b1_key;

        public MainForm()
        {
            InitializeComponent();
            serialPort1.Encoding = Encoding.GetEncoding("GB2312");                                  //串口接收编码
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; 
            //
        }


        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            byte[] data = new byte[serialPort1.BytesToRead];                                //定义缓冲区，因为串口事件触发时有可能收到不止一个字节
            serialPort1.Read(data, 0, data.Length);

            for(byte i = 0;i< data.Length;i++)
            {
                if (data[i] == 144)
                {
                    temp = 1;
                    us_i = 0;
                }
                if (data[i] == 170)
                    aa++;
                if(temp == 1)
                {
                    usartget_data[us_i++] = data[i];
                }
                if (aa == 6)
                    break;

            }
            if(aa >= 6)
            {
                //for (byte i = 0; i < us_i; i++)                                                   //遍历用法
                //{

                //    string str = Convert.ToString(usartget_data[i], 16).ToUpper();
                //    textBox1.AppendText("0x" + (str.Length == 1 ? "0" + str : str) + " ");
                //}
                updata_USART = 1;
                aa = 0;
                temp = 0;
                us_i = 0;
            }
            

            

            //定义缓冲区，因为串口事件触发时有可能收到不止一个字节



        }







        private void comboBox3_5_SelectedIndexChanged(object sender, EventArgs e)
        {
            string mystr = comboBox3.Text;
            string str2;
            for (int k = 0;k<6;k++)
            {
                str2 = (string)comboBox3.Items[k];
                int result = mystr.CompareTo(str2);
                if (result == 0)
                {
                    P_unit = (byte)(k + 161);
                    cod2[2] = P_unit;
                    change_unit = 1;
                    break;
                }
                    
            }

        }



        

        private void comboBox4_6_SelectedIndexChanged(object sender, EventArgs e)
        {
            string mystr = comboBox4.Text;
            string str2;
            for (int k = 0; k < 2; k++)
            {
                str2 = (string)comboBox4.Items[k];
                int result = mystr.CompareTo(str2);//字符串的比较  相等等于0  
                if (result == 0)
                {
                    T_unit = (byte)(k + 161);
                    cod3[2] = T_unit;
                    change_unit = 2;
                    break;
                }

            }
        }

       

        private void MainForm_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 20; i++)
            {
                try
                {
                    serialPort1.PortName = "COM" + i.ToString();
                    serialPort1.Open();
                    serialPort1.Close();
                    comboBox1.Items.Add("COM" + i.ToString());
                }
                catch { }
                                                        //添加串口
            }
            try
            {
                comboBox1.Text = (string)comboBox1.Items[0];
            }
            catch { }
            comboBox2.Text = "9600";
            b1_key = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            if (updata_USART == 1)
            {
                try
                {
                    switch (usartget_data[1])
                    {
                        case 6://0x06
                            start = 2;
                            break;
                        case 25://0x19识别传感器

                            if ((usartget_data[2] > 160) && (usartget_data[2] < 165))
                            {
                                No = usartget_data[2];
                            }
                            if(No == 161)//TPD2000
                            {
                                groupBox2.Text = "绝压(TPD2000)";
                                groupBox3.Text = "全压(TPD2000)";

                            }
                            else if(No == 162)//tp1200
                            {
                                groupBox2.Text = "绝压(TP1200)";
                                groupBox3.Text = "全压";
                            }
                            else if (No == 163)//tp1000
                            {
                                groupBox2.Text = "绝压(TP1000)";
                                groupBox3.Text = "全压";
                            }
                            else if (No == 164)//tip
                            {
                                groupBox2.Text = "绝压(TP1000)";
                                groupBox3.Text = "表压(TIP)";
                            }
                            start = 3;
                            break;
                        case 16://0x10压力单位

                            if ((usartget_data[2] > 160) && (usartget_data[2] < 166))
                            {
                                P_unit = usartget_data[2];
                            }
                            comboBox3.Text = (string)comboBox3.Items[P_unit - 161];
                            if((No == 161)||(No == 164))
                                comboBox5.Text = (string)comboBox5.Items[P_unit - 161];
                            start = 4;
                            break;
                        case 32://0x20温度单位

                            if ((usartget_data[2] > 160) && (usartget_data[2] < 163))
                            {                                
                                T_unit = usartget_data[2];
                            }
                            comboBox4.Text = (string)comboBox4.Items[T_unit - 161];
                            if ((No == 161) || (No == 164))
                                comboBox6.Text = (string)comboBox6.Items[T_unit - 161];
                            start = 5;
                            break;
                        case 1://0x1读压力与温度
                            /***************  pt ******************/
                            if (usartget_data[2] >= 240)//判断是否为负数
                            {
                                if (usartget_data[4] >= 1)
                                    ps_p = 65535 * (byte)~usartget_data[2] + 256 * (byte)~usartget_data[3] + (byte)~(usartget_data[4] - 1);
                                else if (usartget_data[3] < 1)
                                    ps_p = 65535 * (byte)~usartget_data[2] + 256 * (byte)~(usartget_data[3] - 1);
                                else
                                    ps_p = 65535 * (byte)~(usartget_data[2] - 1);
                            }
                            else
                                ps_p = 65535 * usartget_data[2] + 256 * usartget_data[3] + usartget_data[4];

                            if (usartget_data[5] >= 240)
                            {
                                if (usartget_data[6] >= 1)
                                    ps_t = 256 * (byte)~usartget_data[5] + (byte)~(usartget_data[6] - 1);
                                else
                                    ps_t = 256 * (byte)~(usartget_data[5] - 1) + (byte)~(usartget_data[6] - 1);
                            }
                            else
                                ps_t = 256 * usartget_data[5] + usartget_data[6];

                            /*****************   qc   *********************/
                            if ((No == 161) || (No == 164))
                            {
                                if (usartget_data[7] >= 240)//判断是否为负数
                                {
                                    if (usartget_data[9] >= 1)
                                        qc_p = 65535 * (byte)~usartget_data[7] + 256 * (byte)~usartget_data[8] + (byte)~(usartget_data[9] - 1);
                                    else if (usartget_data[8] < 1)
                                        qc_p = 65535 * (byte)~usartget_data[7] + 256 * (byte)~(usartget_data[8] - 1);
                                    else
                                        qc_p = 65535 * (byte)~(usartget_data[7] - 1);
                                }
                                else
                                    qc_p = 65535 * usartget_data[7] + 256 * usartget_data[8] + usartget_data[9];

                                if (usartget_data[10] >= 240)
                                {
                                    if (usartget_data[11] >= 1)
                                        qc_t = 256 * (byte)~usartget_data[10] + (byte)~(usartget_data[11] - 1);
                                    else
                                        qc_t = 256 * (byte)~(usartget_data[10] - 1) + (byte)~(usartget_data[11] - 1);
                                }
                                else
                                    qc_t = 256 * usartget_data[10] + usartget_data[11];
                                textBox5.Text = (qc_p / 100).ToString() + "." + (ps_p % 100).ToString();//qc_p
                                textBox6.Text = (qc_t / 100).ToString() + "." + (ps_p % 100).ToString();//qc_t
                                textBox1.Text = (65535 * usartget_data[12] + 256 * usartget_data[13] + usartget_data[14]).ToString();
                                textBox2.Text = (65535 * usartget_data[15] + 256 * usartget_data[16] + usartget_data[17]).ToString();
                                textBox7.Text = (65535 * usartget_data[18] + 256 * usartget_data[19] + usartget_data[20]).ToString();
                                textBox8.Text = (65535 * usartget_data[21] + 256 * usartget_data[22] + usartget_data[23]).ToString();
                            }
                            else
                            {
                                textBox1.Text = (65535 * usartget_data[7] + 256 * usartget_data[8] + usartget_data[9]).ToString();
                                textBox2.Text = (65535 * usartget_data[10] + 256 * usartget_data[11] + usartget_data[12]).ToString();
                               
                            }
                            textBox3.Text = (ps_p / 100).ToString() + "." + (ps_p % 100).ToString();// +us_t.ToString() ;//ps_p
                            textBox4.Text = (ps_t / 100).ToString() + "." + (ps_p % 100).ToString();//ps_t
                            textBox10.AppendText((ps_p / 100).ToString() + "." + (ps_p % 100).ToString()+ Environment.NewLine);//Environment.NewLine -->换行

                            if (us_t>8)
                                us_t = 0;
                            us_t++;

                            

                            if (change_unit == 1)
                                start = 7;
                            else if (change_unit == 2)
                                start = 8;
                            else
                                start = 6;
                            change_unit = 0;
                            break;
                    }
                    switch (start)
                    {
                        case 2:
                            serialPort1.Write(cod1, 0, 3);
                            break;
                        case 3:
                            serialPort1.Write(cod2, 0, 3);
                            break;
                        case 4:
                            serialPort1.Write(cod3, 0, 3);
                            break;
                        case 5:
                            serialPort1.Write(cod4, 0, 3);
                            break;
                        case 6:
                            serialPort1.Write(cod4, 0, 3);
                            break;
                        case 7://改变压力单位
                            serialPort1.Write(cod2, 0, 4);
                            cod2[2] = 170;
                            break;
                        case 8://改变温度单位
                            serialPort1.Write(cod3, 0, 4);
                            cod3[2] = 170;
                            break;

                    }
                    updata_USART = 0;
                }
                catch
                {
                   // MessageBox.Show("初始化失败!错误代码:  " + start.ToString(), "Error");
                    switch (start)//解决初始化失败,不能解决数据停止
                    {
                        case 0: serialPort1.Write(cod0, 0, 3); break;
                        case 2: serialPort1.Write(cod1, 0, 3); break;
                        case 3: serialPort1.Write(cod2, 0, 3); break;
                        case 4: serialPort1.Write(cod3, 0, 3); break;
                        case 5: serialPort1.Write(cod4, 0, 3); break;
                        case 6: serialPort1.Write(cod4, 0, 3); break;
                    }
                    updata_USART = 0;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (b1_key == 0)
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;                                              //端口号
                    serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);                             //波特率
                    serialPort1.Open();                                                                 //打开串口
                    button1.Text = "重新加载传感器";
                    button2.Enabled = true;
                    
                }
                catch
                {
                    MessageBox.Show("端口错误", "错误");
                }
                serialPort1.Write(cod0, 0, 3);
                timer1.Start();
                b1_key = 1;
            }
            else
            {
                timer1.Stop();
                button1.Enabled = false;
                serialPort1.Close();
                serialPort1.PortName = comboBox1.Text;                                              //端口号
                serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);                             //波特率
                serialPort1.Open();
                button1.Enabled = true;
                us_i = 0;
                updata_USART = 0;
                start = 0;
                aa = 0;
                temp = 0;
                serialPort1.Write(cod0, 0, 3);
                timer1.Start();
            }

        }

  

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                timer1.Stop();
                serialPort1.Close();                                                            //关闭串口        
                button1.Text = "打开串口";
                button2.Enabled = false;
                b1_key = 0;
                textBox10.Clear();
            }
            catch
            {

            }
        }

    }
}
